package com.lichuandi;

// Spring Boot 客户端
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

public class PythonClient {
    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate();
//        http://192.168.0.173:12345/api/control
        String url = "http://192.168.0.173:12345/api/control";
//        String url = "http://127.0.0.1:5000/";
//        String data = {"class_name": "KemaiLightController", "mode": "initialize",'function_name':""}
//        String response = restTemplate.postForObject(url, "Hello from Java", String.class);
//        String response = restTemplate.getForObject(url, String.class);
//        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url, HttpEntity.EMPTY, String.class);
//        String response = stringResponseEntity.getBody();
//        System.out.println(response);


        // 正确地格式化JSON字符串
        String data = "{\"class_name\": \"KemaiLightController\", \"mode\": \"initialize\", \"function_name\": \"\"}";

        // 设置请求头为JSON类型
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        // 使用HttpEntity包装请求体和头信息
        HttpEntity<String> entity = new HttpEntity<>(data, headers);

         //发送POST请求并接收响应
        String response = restTemplate.postForObject(url, entity, String.class);

        System.out.println(response);
    }
}